package com.tian.core.action;

import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tian.core.bean.Condition;
import com.tian.core.bean.PageList;
import com.tian.core.bean.Pages;

@ParentPackage("json-default")//加载json 
@Results( {  
	@Result(type = "json",name="success",params={"ignoreHierarchy","false","noCache","true"})
})
public abstract class JqGridBaseAction extends ActionSupport{
	
	private static final long serialVersionUID = -5944111996813616987L;
	
	protected String id;
	
	// 和jqGrid组件相关的参数属性  
    protected int rows = 0;  //  表示请求行数的参数名称
    protected int page = 0;  // 表示请求页码的参数名称
    protected int total = 0;  // json中代表页码总数的数据
    protected long record = 0;  // json中代表数据行总数的数据
    protected String sord;  // 表示采用的排序方式的参数名称
    protected String sidx;  // 表示用于排序的列名的参数名称
    protected boolean search;  // 表示是否是搜索请求的参数名称
    //添加和查询有关的成员变量search、searchField、searchString、searchOper
    protected String searchField;  
    protected String searchString;  
    protected String searchOper;
    
    protected PageList pageList;
    protected List<Condition> conditions = new ArrayList<Condition>();
       
   
    public abstract String refreshGridModel();

	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public long getRecord() {
		return record;
	}

	public void setRecord(long record) {
		this.record = record;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public boolean getSearch() {
		return search;
	}

	public void setSearch(boolean search) {
		this.search = search;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public List<Condition> getConditions() {
		return conditions;
	}

	public void setConditions(List<Condition> conditions) {
		this.conditions = conditions;
	}


	public String getSearchField() {
		return searchField;
	}


	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	} 
	public String ajax(Object object){
		try{
			JSONArray jsonArray = new JSONArray();
			String json;
			if(object instanceof String){
				json=(String)object;
			}else if( object instanceof Collection){
				jsonArray.addAll((Collection)object);
				json=jsonArray.toString();
			}else if( object instanceof JSONObject){
				json=((JSONObject)object).toString();
			}else if( object instanceof JSONArray){
				json=((JSONArray)object).toString();
			}else if( object instanceof Boolean){
				Boolean b = (Boolean)object;
				if(b){
					json = "true";
				}else{
					json = "false";
				}
			}else{
				JSONObject jsonObject = JSONObject.fromObject(object);
				json=jsonObject.toString();
			}
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setContentType("application/json;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			Writer out = resp.getWriter();
			out.append(json);
			out.flush();
		}catch (Exception e) {
			System.out.println(e);
		}
		
		return NONE;
	}
    
}
